package com.qf.service.impl;

import com.qf.dao.OrderDao;
import com.qf.dao.OrderDetailDao;
import com.qf.dao.UserDao;
import com.qf.dao.impl.OrderDaoImpl;
import com.qf.dao.impl.OrderDetailDaoImpl;
import com.qf.dao.impl.UserDaoImpl;
import com.qf.domain.Cart;
import com.qf.domain.Order;
import com.qf.domain.OrderDetail;
import com.qf.domain.User;
import com.qf.service.CartService;
import com.qf.service.OrderService;
import com.qf.utils.RandomUtils;
import com.qf.utils.TxUtils;
import jdk.nashorn.internal.objects.annotations.Where;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @author YDW
 * projectName:shopping
 * time:2022/4/21 9:24
 * description:
 */
public class OrderServiceImpl implements OrderService {
    CartService cartService=new CartServiceImpl();
    OrderDao orderDao=new OrderDaoImpl();
    OrderDetailDao orderDetailDao=new OrderDetailDaoImpl();
    UserDao userDao=new UserDaoImpl();
    //添加订单
    /*
    * 生成一条订单记录
    * 添加订单详情
    * 删除购物车
    * */
    @Override
    public Order addOrder(String aid, int uid) {
        Connection conn = TxUtils.getConn();
        try {
            conn.setAutoCommit(false);
            //获取用户购物车信息
            List<Cart> carts = cartService.findCartByUid(uid);
            double sum=0;
            for (Cart cart : carts) {
                sum+=cart.getMoney();

            }
            Order order=new Order();
            //设置订单
            order.setId(RandomUtils.createActive());
            order.setUid(uid);
            order.setMoney(sum);
            order.setStatus("1");
            order.setTime(new Date());
            order.setAid(Integer.parseInt(aid));
            orderDao.insert(order);
            //添加订单详情
            //一个购物车商品对应一个订单详情
            for (Cart cart : carts) {
                OrderDetail orderDetail=new OrderDetail();
                orderDetail.setOid(order.getId());
                orderDetail.setPid(cart.getPid());
                orderDetail.setNum(cart.getNum());
                orderDetail.setMoney(cart.getMoney());
                orderDetailDao.insert(orderDetail);
            }
            //删除购物车
            cartService.clearCartByUid(uid);
            conn.commit();
            return order;
        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            TxUtils.remove();
            try {
                conn.setAutoCommit(true);
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public void paySuccess(Order order) {
        orderDao.paySuccess(order);
    }

    @Override
    public List<Order> findAllOrder() {
        List<Order> orders=orderDao.selectAllOrder();
        for (Order order : orders) {
            int uid = order.getUid();
            User user=userDao.finduser(uid);
            order.setUser(user);
        }
        return orders;
    }

    @Override
    public void changestatus(String oid) {
        orderDao.changestatus(oid);
    }

    @Override
    public Order searchOrder(String where, List<Object> params, int id) {
        Order order=orderDao.searchOrder(where,params,id);
        return order;
    }


    @Override
    public List<Order> searchOrderByUid(int id) {
        List<Order> orders=orderDao.searchOrderByUid(id);
        for (Order order : orders) {
            User user = userDao.finduser(order.getUid());
            order.setUser(user);
        }
        return orders;
    }

    @Override
    public List<Order> searchOrderByStatus(String status) {
        List<Order> orders=orderDao.searchOrderByStatus(status);
        for (Order order : orders) {
            User user = userDao.finduser(order.getUid());
            order.setUser(user);
        }
        return orders;
    }

    @Override
    public List<Order> searchOrderByUidAndStatus(int id, String status) {
        List<Order> orders=orderDao.searchOrderByUidAndStatus(id,status);
        for (Order order : orders) {
            User user = userDao.finduser(order.getUid());
            order.setUser(user);
        }
        return orders;
    }
}
